package app
{
		import app.db.GoRemoteConnection;
		import data.CONST;
		import flash.events.Event;
		import flash.events.MouseEvent;
		import flash.net.Responder;
		import mx.controls.Alert;
		import mx.utils.ObjectUtil;

	
	/**
	 * @author thomas fricke
	 * @version 1.0.0
	 * @date 2009-06-11
	 * @desc a GoLoginHandler handles the login data a certain user has entered.
	 * 		 if the entered data is equal to the corresponding database entry,
	 * 		 the user gets logged in.
	 **/
	public class GoLoginHandler
	{
		
		public function comp():void {

		}
			// http://www.flashfocus.nl/forum/showthread.php?t=33175 <-- thanks a lot, guys!
		
		[Bindable] private var itemGetter:GoItemGetter = GoItemGetter.getSingleton();
		public var gateway:GoRemoteConnection;
		private var GATEWAY_URL:String = CONST.GATEWAY_LOC;
		private var __login:String;
		private var __pass:String;
		private var U_ID:uint;
		
		// constructor:
		public function Login():void {
			gateway = new GoRemoteConnection(CONST.GATEWAY_LOC);
		}
			
		public function submitForm(user:String, pass:String):void {
			//CONST.UID = 0; 		// resetting the user id before login
			gateway = new GoRemoteConnection(CONST.GATEWAY_LOC);
			
			__login = user;
			__pass  = pass;
			gateway.connect(GATEWAY_URL);
			gateway.call("GoLoginHandler.doLogin", new Responder(handleDoLogin, faultHandler), __login, __pass);
			// gateway.close();
		}
		
		public function handleDoLogin(re:*):void {
			if(!(re is Boolean)) {
				CONST.IS_LOGGED_IN = true;
				state = true;
				this.dispatchEvent(new Event("loginChanged"));
				CONST.UNAME = re['uname'] as String;
				CONST.USTATE = re['ustate'] as String;
				CONST.UID = re['uid'];
				trace("\'---> logged in with your user id: " + CONST.UID + "and user name: " + CONST.UNAME);
				itemGetter.getCategoryList();
			} else if(re is Boolean) {
				CONST.IS_LOGGED_IN = false;
				Alert.show("Login failed!");
			}
			
		}
			
		private function faultHandler(evt:Event):void {
			trace("\'---o login fault event: " + ObjectUtil.toString(evt));
		}
		
		[Bindable] private var logonstate:Boolean;
		public function get state():Boolean {
			return logonstate;
		}
		
		public function set state(value:Boolean):void {
			if(this.logonstate == value) {
				return;
			}
			logonstate = value;
		}


		
		private function goBackHandler(evt:MouseEvent):void {
			//
		}
	}
}